package com.funtl.my.shop.web.admin.abstracts;

import com.funtl.my.shop.domain.TbContent;
import com.funtl.my.shop.domain.TbUser;
import com.futl.my.shop.commons.dto.BaseResult;
import com.futl.my.shop.commons.dto.PageInfo;
import com.futl.my.shop.commons.persistence.BaseEntity;
import com.futl.my.shop.commons.persistence.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tw
 * @version 1.0
 * @date 2020/6/2 20:49
 */
public abstract class AbstractBaseController<T extends BaseEntity, S extends BaseService<T>> {

    @Autowired
    protected S service;

    /**
     * 跳转用户列表
     * @param
     * @return
     */
    public abstract String list();

    public abstract String form();

    public abstract String detail(T entity);

    /**
     * 新增用户
     * @param entity
     * @param model
     * @param redirectAttributes
     * @return
     */
    public abstract String add(T entity, Model model, RedirectAttributes redirectAttributes);

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    public abstract BaseResult delete(String ids);


    /**
     * 分页查询
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "page",method = RequestMethod.POST)
    public PageInfo<T> page(HttpServletRequest request, T entity){
        Map<String, Object> result = new HashMap<>();

        String strDraw = request.getParameter("draw");
        String strStart = request.getParameter("start");
        String strLength = request.getParameter("length");

        int draw = strDraw == null ? 0 : Integer.parseInt(strDraw);
        int start = strDraw == null ? 0 : Integer.parseInt(strStart);
        int length = strDraw == null ? 0 : Integer.parseInt(strLength);

        PageInfo<T> pageInfo = service.page(start, length, draw, entity);
        return pageInfo;
    }
}
